/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.util;

/**
 * 处理'8位无符号char'的转换.
 * 
 * @version 2014年3月8日下午2:23:06
 * @author jrwz
 */
public final class UChar8 {
    /**
     * 将一个32位(unsigned/signed)long数组 转换为 8位无符号char数组.
     * 
     * @param buff
     *            - 要转换的long数组
     * @return char[] 一个8位无符号char数组
     */
    public static char[] fromArray(final long[] buff) {
        final char[] charBuff = new char[buff.length * 4];

        for (int i = 0; i < buff.length; ++i) {
            charBuff[(i * 4) + 0] = (char) (buff[i] & 0xFF);
            charBuff[(i * 4) + 1] = (char) ((buff[i] >> 8) & 0xFF);
            charBuff[(i * 4) + 2] = (char) ((buff[i] >> 16) & 0xFF);
            charBuff[(i * 4) + 3] = (char) ((buff[i] >> 24) & 0xFF);
        }
        return charBuff;
    }

    /**
     * 将一个8位无符号byte数组 转换为 8位无符号char数组.
     * 
     * @param buff
     *            - 要转换的byte数组
     * @return char[] 一个8位无符号char数组
     */
    public static char[] fromArray(final byte[] buff) {
        final char[] charBuff = new char[buff.length];

        for (int i = 0; i < buff.length; ++i) {
            charBuff[i] = (char) (buff[i] & 0xFF);
        }
        return charBuff;
    }

    /**
     * 将一个8位无符号byte数组 转换为 8位无符号char数组.
     * 
     * @param buff
     *            - 要转换的byte数组
     * @param length
     *            - 数组的长度大小
     * @return char[] 一个8位无符号char数组
     */
    public static char[] fromArray(final byte[] buff, final int length) {
        final char[] charBuff = new char[length];

        for (int i = 0; i < length; ++i) {
            charBuff[i] = (char) (buff[i] & 0xFF);
        }
        return charBuff;
    }

    /**
     * 将一个8位无符号byte 转换为 8位无符号char.
     * 
     * @param b
     *            - 要转换的byte
     * @return char 一个8位无符号char
     */
    public static char fromUByte8(final byte b) {
        return (char) (b & 0xFF);
    }

    /**
     * 将一个32位无符号long 转换为 8位无符号char.
     * 
     * @param l
     *            - 要转换的long
     * @return char 一个8位无符号char
     */
    public static char[] fromULong32(final long l) {
        final char[] charBuff = new char[4];

        charBuff[0] = (char) (l & 0xFF);
        charBuff[1] = (char) ((l >> 8) & 0xFF);
        charBuff[2] = (char) ((l >> 16) & 0xFF);
        charBuff[3] = (char) ((l >> 24) & 0xFF);
        return charBuff;
    }
}
